package org.netty.push.core.proto.codec;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

public class CustomerDecoder extends MessageToMessageDecoder<ByteBuf> {
	private static  Logger logger=LoggerFactory.getLogger(CustomerDecoder.class);
	private ObjectMapper objectMapper=new ObjectMapper();
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
		logger.debug("CustomerDecoder......................");
		
		while(msg.readableBytes()>0) {
			int length=msg.readInt();
			byte name[]=new byte[length];
			msg.readBytes(name);
			String class_name=new String(name);
			length=msg.readInt();
			byte body[]=new byte[length];
			msg.readBytes(body);
			Class<?> msg_class=Class.forName(class_name);
			Object obj=objectMapper.readValue(body, msg_class);
			out.add(obj);
		}
	}



}
